package com.molyfun.parents.activity.course;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseChoosePicActivity;
import com.molyfun.parents.activity.course.adapter.CourseFeedbackListAdapter;
import com.molyfun.parents.fragment.dialog.CommonDialogFragment;
import com.molyfun.parents.rest.bean.CourseFeedbackListResponse;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.uievent.UIEventListener;
import com.molyfun.parents.utils.Const;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 */
public class CourseFeedbackListActivity extends BaseChoosePicActivity implements View.OnClickListener, UIEventListener {

    public static java.lang.String TAGS = "CourseFeedbackListActivity";
    @InjectView(R.id.base_title_left)
    ImageView mTitleBack;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmptyLayout;
    @InjectView(R.id.new_feedback)
    TextView newFeedBack;
    @InjectView(R.id.base_title)
            TextView mTitle;

    LinearLayoutManager mLinearLayoutManager;
    CourseFeedbackListAdapter courseFeedbackListAdapter;
    CourseListResponse.CourseBean mCourseBean;

    private String mCourseId;
    private String mTimeStr;

    private List<CourseFeedbackListResponse.CourseFeedbackBean> mDataList;

    public static void startActivity(Context context, String courseId, String timeStr, CourseListResponse.CourseBean courseBean) {
        Intent intent = new Intent();
        intent.putExtra(Const.COURSE_ID, courseId);
        intent.putExtra(Const.COURSE_BEAN, courseBean);
        intent.putExtra(Const.COURSE_FEEDBACK_TIME, timeStr);
        intent.setClass(context, CourseFeedbackListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_feedback_list);
        ButterKnife.inject(this);

        mTitleBack.setOnClickListener(this);
        newFeedBack.setOnClickListener(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("课程反馈");

        mRecyclerView.setHasFixedSize(true);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        mTitleBack.setOnClickListener(this);

        courseFeedbackListAdapter = new CourseFeedbackListAdapter(this, getSupportFragmentManager());
        mRecyclerView.setAdapter(courseFeedbackListAdapter);
        mCourseId = getIntent().getStringExtra(Const.COURSE_ID);
        mCourseBean = getIntent().getParcelableExtra(Const.COURSE_BEAN);
        mTimeStr = getIntent().getStringExtra(Const.COURSE_FEEDBACK_TIME);
        if (mCourseBean != null) {

            if (mCourseBean.getFeedbacktimes() >= 5) {
                newFeedBack.setVisibility(View.GONE);
            }
        }
        MFPApplication.getInstance().getEventController().addUIEventListener(EventDispatcherEnum.UI_EVENT_COURSE_FREEBACK_UPDATE,this);
    }

//    private static CourseFeedbackDialogFragment mCourseFeedbackDialogFragment;
//
//    public static void setCourseFeedbackDialogFragment(CourseFeedbackDialogFragment courseFeedbackDialogFragment) {
//        mCourseFeedbackDialogFragment = courseFeedbackDialogFragment;
//    }


    @Override
    protected void onResume() {
        getFeedbackList();
        super.onResume();
    }

    @Override
    public void handleUIEvent(Message msg) {
        if(msg.what == EventDispatcherEnum.UI_EVENT_COURSE_FREEBACK_UPDATE) {
            //刷新
            getFeedbackList();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.new_feedback:
                if (mCourseBean.getFeedbacktimes() < 5) {
//                    mCourseFeedbackDialogFragment = new CourseFeedbackDialogFragment.CourseFeedbackDialogBuilder(mCourseBean.getId(), mCourseBean.getDkey())
//                            .setCourseFeedbackCallback(new CourseFeedbackDialogFragment.CourseFeedbackCallback() {
//                                @Override
//                                public void onFeedbackSuccess() {
//
//                                    getFeedbackList();
//                                }
//
//                                @Override
//                                public void onFeedbackUpdateSuccess(CourseFeedbackEditRequest courseFeedbackEditRequest) {
//
//                                }
//                            })
//                            .build(getSupportFragmentManager().beginTransaction());

                    CourseFeedbackEditActivity.startActivity(CourseFeedbackListActivity.this, mCourseBean.getId(), mCourseBean.getDkey(), null);

                } else {
                    new CommonDialogFragment.CommonDialogBuilder("最多五条课程反馈，已达到上限")
                            .build(getSupportFragmentManager().beginTransaction());
                }
                break;
        }
    }

    private void getFeedbackList() {
        showProgressDialog();
        Call<CourseFeedbackListResponse> call = MFPApplication.getInstance().getRestClient().getCourseFeedbackList(mCourseId);
        call.enqueue(new Callback<CourseFeedbackListResponse>() {
            @Override
            public void onResponse(Call<CourseFeedbackListResponse> call, Response<CourseFeedbackListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    mDataList = response.body().getData();
                    courseFeedbackListAdapter.refreshData(mCourseBean, mDataList);
                    if (mDataList == null || mDataList.size() == 0) {
                        mEmptyLayout.setVisibility(View.VISIBLE);
                    } else {
                        if (mDataList.size() >= 5) {
                            newFeedBack.setVisibility(View.GONE);
                        }
                    }

                } else {
                    mEmptyLayout.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(Call<CourseFeedbackListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });

    }

    @Override
    public String getAttachmentFolder() {
//        if (mCourseFeedbackDialogFragment != null) {
//            return mCourseFeedbackDialogFragment.getAttachmentFolder();
//        }
        return "coursefb";
    }

    @Override
    public void onPictureUploadSuccess(String localPath, String smallUrl, String originalUrl) {
//        if (mCourseFeedbackDialogFragment != null) {
//            mCourseFeedbackDialogFragment.onPictureUploadSuccess(localPath, smallUrl, originalUrl);
//        }
    }

    public void onPictureUploadBegin(String path, String UploadPath) {

    }

    @Override
    public void onPictureUploadFailed(String path, String UploadPath) {
        toast("图片上传失败");
    }

    @Override
    public void onVideoUploadSuccess(String cover, String path) {
//        if (mCourseFeedbackDialogFragment != null) {
//            mCourseFeedbackDialogFragment.onVideoUploadSuccess(cover, path);
//        }
    }


}
